import platform
import psutil
import GPUtil
from tabulate import tabulate
import wmi

# pip install psutil GPUtil tabulate wmi

def get_system_info():
    uname = platform.uname()
    system_info = {
        "System": uname.system,
        "Node Name": uname.node,
        "Release": uname.release,
        "Version": uname.version,
        "Machine": uname.machine,
        "Processor": uname.processor,
    }
    return system_info


def get_gpu_info():
    gpus = GPUtil.getGPUs()
    list_gpus = []
    for gpu in gpus:
        gpu_id = gpu.id
        gpu_name = gpu.name
        gpu_memoryUsed = f"{gpu.memoryUsed}MB"
        gpu_memoryTotal = f"{gpu.memoryTotal}MB"
        list_gpus.append((gpu_id, gpu_name, gpu_memoryUsed, gpu_memoryTotal))
    return list_gpus


def get_cpu_info():
    cpufreq = psutil.cpu_freq()
    cpu_info = {
        "Physical cores": psutil.cpu_count(logical=False),
        "Total cores": psutil.cpu_count(logical=True),
        "Max Frequency": f"{cpufreq.max:.2f}Mhz",
        "Min Frequency": f"{cpufreq.min:.2f}Mhz",
        "Current Frequency": f"{cpufreq.current:.2f}Mhz",
        "CPU Usage": f"{psutil.cpu_percent(interval=1)}%",
    }
    return cpu_info


def get_hard_drive_info():
    c = wmi.WMI()
    drives = []
    for physical_disk in c.Win32_DiskDrive():
        drive_info = {
            "Caption": physical_disk.Caption,
            "Size": f"{float(physical_disk.Size) / (1024**3):.2f} GB",
        }
        drives.append(drive_info)
    return drives


def get_motherboard_info():
    c = wmi.WMI()
    motherboard = c.Win32_BaseBoard()[0]
    motherboard_info = {
        "Manufacturer": motherboard.Manufacturer,
        "Product": motherboard.Product,
        "Serial Number": motherboard.SerialNumber,
        "Version": motherboard.Version,
    }
    return motherboard_info


def obtain_hardware():
    detail = "System Information:\n"
    detail += tabulate([get_system_info()], headers="keys")
    detail += "\nGPU Information:"
    detail += tabulate(
        get_gpu_info(), headers=("ID", "Name", "Memory Used", "Memory Total")
    )
    detail += "\nCPU Information:"
    detail += tabulate([get_cpu_info()], headers="keys")
    detail += "\nHard Drive Information:"
    detail += tabulate(get_hard_drive_info(), headers="keys")
    detail += "\nMotherboard Information:"
    detail += tabulate([get_motherboard_info()], headers="keys")
    detail += "\n\n"
    return detail


def obtain_by_wmi():
    # 初始化WMI接口
    c = wmi.WMI()

    # 获取电脑型号
    computer_system = c.Win32_ComputerSystem()[0]
    computer_model = computer_system.Model
    computer_manufacturer = computer_system.Manufacturer

    # 获取处理器信息
    processor_info = c.Win32_Processor()[0]
    processor_name = processor_info.Name
    processor_speed = processor_info.MaxClockSpeed

    # 获取内存信息
    memory_info = c.Win32_PhysicalMemory()[0]
    memory_size = int(memory_info.Capacity) / (1024 * 1024 * 1024)

    # 获取显卡信息
    gpu_info = c.Win32_VideoController()[0]
    gpu_name = gpu_info.Name
    gpu_driver = gpu_info.DriverVersion

    detail = f"Computer model: {computer_model}\n"
    detail += f"manufacturer: {computer_manufacturer}\n"
    detail += f"Processor Name: {processor_name}\n"
    detail += f"Processor speed: {processor_speed} MHz\n"
    detail += f"Memory size: {memory_size:.2f} GB\n"
    detail += f"Graphics card name: {gpu_name}\n"
    detail += f"Graphics card driver version: {gpu_driver}\n"
    return detail


def save_detail():
    # 要写入的字符串内容
    content = obtain_hardware()
    content += obtain_by_wmi()
    print(content)

    # 打开文件并覆盖写入
    file_name = "z-information.txt"
    with open(file_name, "w") as file:
        file.write(content)

    print(f"内容已成功写入到{file_name}文件中")


save_detail()
